package xin.marcher.module.quality.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.enums.inventory.InventoryExceptionCode;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.quality.domain.dto.ItemQualityControlDataDTO;
import xin.marcher.module.quality.domain.dto.ItemQualityControlSurveyDTO;
import xin.marcher.module.quality.domain.dto.ItemQualityControlSurveyResultDTO;
import xin.marcher.module.quality.domain.request.ItemQualityControlSurveyRequest;
import xin.marcher.module.quality.repository.ItemQualityControlRepository;
import xin.marcher.module.quality.repository.OperatorCategoryRelationRepository;
import xin.marcher.module.quality.service.ItemQualityControlSurveyService;
import xin.marcher.module.rights.support.LoginUser;
import xin.marcher.module.rights.support.LoginUserHolder;

import java.util.List;
import java.util.Objects;

import static xin.marcher.module.common.constants.diplomacy.BaseConstants.PAGE_SIZE_100;

/**
 *
 */
@Service
public class ItemQualityControlSurveyServiceImpl implements ItemQualityControlSurveyService {
    @Autowired
    private ItemQualityControlRepository itemQualityControlRepository;

    @Autowired
    private OperatorCategoryRelationRepository operatorCategoryRelationRepository;

    /**
     * 调查池列表
     *
     * @param request
     * @return
     */
    @Override
    public List<ItemQualityControlSurveyDTO> surveyPoolList(ItemQualityControlSurveyRequest request) {
        // 参数检查
        checkQueryParams(request);
        // 获取用户所属categoryId
        List<Integer> categoryIdList = operatorCategoryRelationRepository.queryCategoryIdByUserId();
        request.setCategoryIdList(categoryIdList);
        return itemQualityControlRepository.surveyPoolList(request);
    }

    /**
     * 加入调查池
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ItemQualityControlSurveyResultDTO joinSurveyPool(ItemQualityControlSurveyRequest request) {
        // 参数检查
        checkParams(request);
        // 获取用户所属categoryId
        List<Integer> categoryIdList = operatorCategoryRelationRepository.queryCategoryIdByUserId();
        request.setCategoryIdList(categoryIdList);
        // 构造参数
        ItemQualityControlDataDTO itemQualityControlDataDTO = getQualityDataDTO(request);
        // 加入调查池
        itemQualityControlRepository.joinSurveyPool(request);
        // 使风险商品列表中的数据失效
        itemQualityControlRepository.invalidQualityData(itemQualityControlDataDTO);
        return new ItemQualityControlSurveyResultDTO(Boolean.TRUE);
    }

    /**
     * 参数检查
     *
     * @param request
     */
    private void checkParams(ItemQualityControlSurveyRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getSurveyId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 分页查询参数检查
     *
     * @param request
     */
    private void checkQueryParams(ItemQualityControlSurveyRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (request.getPageSize() > PAGE_SIZE_100) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 构造参数
     *
     * @param request
     * @return
     */
    private ItemQualityControlDataDTO getQualityDataDTO(ItemQualityControlSurveyRequest request) {
        // 获取用户ID
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (Objects.isNull(loginUser)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
        Long userId = loginUser.getUserId();

        ItemQualityControlDataDTO itemQualityControlDataDTO = new ItemQualityControlDataDTO();
        itemQualityControlDataDTO.setUserId(userId);
        itemQualityControlDataDTO.setCategoryIdList(request.getCategoryIdList());
        return itemQualityControlDataDTO;
    }
}
